package org.platform.processor.task.check.impl;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import org.platform.dao.mapper.FdSysconfigMapper;
import org.platform.mo.bussmo.task.CheckItemResult;
import org.platform.processor.task.check.ICheckItem;

import java.io.*;

/**
 * 4.	检查天线的锁定是否满足阈值要求
 * <p>
 * 检查步骤:
 * 1.判断天线所在的  25)卫星频段
 * 2.根据卫星频段 判断各个频段所在的锁定情况:   16)S频段锁定指示/20)X频段锁定指示/24)Ka频段锁定指示
 *
 * @author FengJie
 * @date 2018/7/6
 */
public class AntennaLockingCheckItemImpl implements ICheckItem {
    /**
     * 锁定状态
     */
    private static final String UN_LOCKING = "失锁";
    /**
     * 3种卫星频段
     */
    private static final String S = "S";
    private static final String X = "X";
    private static final String KA = "Ka";
    /**
     * 各 频段所在的列数
     */
    private static final int S_FREQUENCY_COLUMN = 16 - 1;
    private static final int X_FREQUENCY_COLUMN = 20 - 1;
    private static final int KA_FREQUENCY_COLUMN = 24 - 1;
    private static final int FREQUENCY_COLUMN = 25 - 1;
    /**
     * 各 频段锁定指示 对应的数据库key
     */
    private static final String S_FREQUENCY_VALUE_KEY = "sFrequencyValue";
    private static final String X_FREQUENCY_VALUE_KEY = "xFrequencyValue";
    private static final String KA_FREQUENCY_VALUE_KEY = "kaFrequencyValue";

    private FdSysconfigMapper sysConfigMapper;

    public AntennaLockingCheckItemImpl() {
        this.sysConfigMapper = SpringContextHelper.getBean(FdSysconfigMapper.class);
    }


    @Override
    public CheckItemResult getCheckResult(String taskSerialNumber, String jobTaskID, File file) throws IOException {
        CheckItemResult result = new CheckItemResult(true, "正常");

        double sValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(S_FREQUENCY_VALUE_KEY).getCurrentvalue());
        double xValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(X_FREQUENCY_VALUE_KEY).getCurrentvalue());
        double kaValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(KA_FREQUENCY_VALUE_KEY).getCurrentvalue());
        double count = 0;

        double sErrorCount = 0;
        double xErrorCount = 0;
        double kaErrorCount = 0;


        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        reader.readLine();
        String line;
        while ((line = reader.readLine()) != null) {
            ++count;
            String[] split = line.split("\t");
            //卫星频段
            String antennaFrequency = split[FREQUENCY_COLUMN];
            switch (antennaFrequency) {
                case S:
                    String s = split[S_FREQUENCY_COLUMN];
                    if (s.equals(UN_LOCKING)) {
                        sErrorCount++;
                    }
                    break;
                case X:
                    String x = split[X_FREQUENCY_COLUMN];
                    if (x.equals(UN_LOCKING)) {
                        xErrorCount++;
                    }
                    break;
                default:
                    String ka = split[KA_FREQUENCY_COLUMN];
                    if (ka.equals(UN_LOCKING)) {
                        kaErrorCount++;
                    }
            }
        }
        reader.close();
        inputStreamReader.close();
        fileInputStream.close();

        //判断结果
        if (sErrorCount / count > sValue) {
            result.setStatus(false);
            result.setConclusion("天线S频段锁定异常");
        }
        if (xErrorCount / count > xValue) {
            result.setStatus(false);
            result.setConclusion("天线X频段锁定异常");
        }
        if (kaErrorCount / count > kaValue) {
            result.setStatus(false);
            result.setConclusion("天线KA频段锁定异常");
        }
        return result;
    }

}
